package com.wteam.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wteam.PoJo.DO.Voucher;
import com.wteam.PoJo.DO.VoucherUser;
import com.wteam.PoJo.DO.user;
import com.wteam.PoJo.VO.MyVoucherVO;
import com.wteam.PoJo.VO.VoucherVO;
import com.wteam.controller.VoucherUserController;
import com.wteam.framework.common.exception.RetryException;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.mapper.VoucherUserMapper;
import com.wteam.Service.IVoucherUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import com.wteam.until.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户关联优惠券表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Slf4j
@Service
public class VoucherUserServiceImpl extends ServiceImpl<VoucherUserMapper, VoucherUser> implements IVoucherUserService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addvoucher(Long vocherid) {

        Long userid = UserContext.getUser();

        user pointuser = Db.lambdaQuery(user.class).
                eq(user::getId, userid).
                select(user::getPoints).
                one();
        log.info(pointuser.toString());
        Voucher voucher = Db.lambdaQuery(Voucher.class)
                .eq(Voucher::getId, vocherid)
                .one();

        if (pointuser.getPoints() < voucher.getPoints()) {
            throw new ServiceException("积分不够");
        }
        //领取数量+1
        Db.lambdaUpdate(Voucher.class).eq(Voucher::getId, vocherid)
                .set(Voucher::getRecivedNum, voucher.getRecivedNum() + 1)
                .eq(Voucher::getRecivedNum, voucher.getRecivedNum()).update();

        Integer uppoint = pointuser.getPoints() - voucher.getPoints();
        //该用户积分数量
        Db.lambdaUpdate(user.class)
                .set(user::getPoints, uppoint)
                .eq(user::getPoints, pointuser.getPoints())
                .update();
        //给用户一张优惠卷
//        VoucherUser voucherUser1 = new VoucherUser();
//        voucherUser1.setVoucherId(vocherid);
//        voucherUser1.setUserId(userid);
////        voucherUser1.setNum(1);
//        save(voucherUser1);

        VoucherUser voucherUser = lambdaQuery().eq(VoucherUser::getUserId, userid)
                .eq(VoucherUser::getVoucherId,vocherid).one();

        if (BeanUtil.isEmpty(voucherUser)) {
            VoucherUser voucherUser1 = new VoucherUser();
            voucherUser1.setVoucherId(vocherid);
            voucherUser1.setUserId(userid);
            voucherUser1.setNum(1);
            save(voucherUser1);
            return Result.success("兑换成功");
        }
        lambdaUpdate().set(VoucherUser::getNum, voucherUser.getNum() + 1)
                .eq(VoucherUser::getUserId, userid)
                .eq(VoucherUser::getVoucherId,vocherid)
                .update();


        return Result.success("兑换成功");
    }

    @Override
    public Result selectvoucher() {

        //常驻优惠
        List<Voucher> list = Db.lambdaQuery(Voucher.class).eq(Voucher::getType, 1).list();

        //非 .常驻优惠卷
        List<Voucher> list1 = Db.lambdaQuery(Voucher.class).ge(Voucher::getEndTime, LocalDateTime.now())
                .le(Voucher::getStartTime, LocalDateTime.now()).eq(Voucher::getType, 0).list();
        list1 = list1.stream().filter(o -> o.getRecivedNum() < o.getMaxNum()).collect(Collectors.toList());
        list1.addAll(list);

        List<VoucherVO> voucherVOS = BeanUtil.copyToList(list1, VoucherVO.class);
        return Result.success(voucherVOS);
    }

    @Override
    public Result myvocher() {
        Long userid = UserContext.getUser();
        List<VoucherUser> list = lambdaQuery()
                .eq(VoucherUser::getUserId, userid)
                .ge(VoucherUser::getNum,1)
                .list();
        if (CollUtil.isEmpty(list)){
            return Result.error("优惠卷为空");
        }

        Map<Long, Integer> collect = list.stream().collect(Collectors.toMap
                (VoucherUser::getVoucherId, VoucherUser::getNum));
        Set<Long> Voucherid = collect.keySet();
        List<Voucher> list1 = Db.lambdaQuery(Voucher.class)
                .in(Voucher::getId, Voucherid).list();

        List<MyVoucherVO> myVoucherVOS = BeanUtil.copyToList(list1, MyVoucherVO.class);

        for (MyVoucherVO myVoucherVO : myVoucherVOS) {
            myVoucherVO.setNum(collect.get(myVoucherVO.getId()));
        }

        return Result.success(myVoucherVOS);
    }
}
